#pragma once

// #include <boost/logic/tribool.hpp>
// #include <boost/tuple/tuple.hpp>
#include "RawPackage.h"
#include <boost/shared_ptr.hpp>

/// Parser for incoming requests.
class RawPackageParser
{
public:
	/// Construct ready to parse the request method.
	RawPackageParser();

	/// Reset to initial parser state.
	void reset();


	template <typename InputIterator, typename PackageHandler>
	bool parse(InputIterator begin, InputIterator end, PackageHandler& handler)
	{
		while (begin != end)
		{
			if (consume(package_, *begin++))
			{
				//handle package
				handler(package_);
				package_.clear();
			}
		}

		return true;
	}

private:
	/// Handle the next character of input.
	bool consume(RawPackage& package, const char input);


	/// The current state of the parser.
	enum state
	{
		package_size_1,
		package_size_2,
		package_buffer
	} state_;

	uint16 package_size_;
	RawPackage package_;


};
